/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LineWatches.java
 *
 * Created on Mar 27, 2009, 1:10:16 AM
 */

package PET;

import PET.model.LineWatches;
import PET.model.PersistentSettings;
import PET.model.SettingsMgr;
import java.awt.Font;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author crayment
 */
public class LineWatchView extends javax.swing.JPanel implements Observer{

    public static final String ALL = "All";

    private DefaultTableModel table;
    private Vector data = new Vector();


    
    /** Creates new form LineWatches */
    public LineWatchView() {
        initComponents();

        Vector colNames = new Vector();
        colNames.add("Line"); colNames.add("Number of executions");

        table = new DefaultTableModel(data, colNames);

        Vector firstRow = new Vector();
        firstRow.add(ALL); firstRow.add(0);

        table.addRow(firstRow);

        LineWatchTB.setModel(table);

        updateView();

        setCustomSettings();
    }


    public void update(Observable o, Object arg) {
            
            int index = getIndexOf(arg);

            if(arg.equals("All")){
                int executions = LineWatches.get(arg);
                updateLine(arg, executions);
                return;
            }

            if(LineWatches.contains((Integer)arg) && index < 0){
                int executions = LineWatches.get(arg);
                addLine(arg, executions);
                return;
            }
            if(LineWatches.contains((Integer)arg) && index >= 0){
                int executions = LineWatches.get(arg);
                updateLine(arg, executions);
                return;
            }
            removeLine(arg);


    }
    
    
    private void updateLine(Object line, int executionCount) {
        int index = getIndexOf(line);
       
        Vector row = (Vector) data.elementAt(index);
        row.setElementAt(executionCount, 1);
       

        updateView();

    }

    private void addLine(Object line, int executionCount){
         Vector row = new Vector();
         row.add(line); row.add(executionCount);
         data.add(row);
         updateView();
    }

    private void removeLine(Object line){
        int index = getIndexOf(line);
        data.removeElementAt(index);
        updateView();
    }


    private int getIndexOf(Object line)
    {
        for(int i=0; i<data.size(); i++)
        {
            Vector row = (Vector)data.elementAt(i);
            if(row.elementAt(0).equals(line))
                return i;
        }
        return -1;
    }


    private void updateView()
    {
        table.fireTableDataChanged();
    }

public void setCustomSettings()
    {
        try {
            PersistentSettings ps = new PersistentSettings();
            SettingsMgr sm = new SettingsMgr();
            ps = (PersistentSettings) sm.readObject(ps.getClass().getName());
            if (ps == null) //Write the default object
            {
                ps = new PersistentSettings();
                sm.writeObject(ps);
            }
            Font font = new Font(ps.getFontType(), Font.PLAIN, ps.getFontSize());
            this.LineWatchTB.setFont(font);
        }

        catch (IOException ex)
        {
            Logger.getLogger(Terminal.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(LineNumberedTextArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LineWatchSP = new javax.swing.JScrollPane();
        LineWatchTB = new javax.swing.JTable();

        setName("Form"); // NOI18N

        LineWatchSP.setName("LineWatchSP"); // NOI18N

        LineWatchTB.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        LineWatchTB.setName("LineWatchTB"); // NOI18N
        LineWatchSP.setViewportView(LineWatchTB);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LineWatchSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LineWatchSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane LineWatchSP;
    private javax.swing.JTable LineWatchTB;
    // End of variables declaration//GEN-END:variables


    /**
     * @return the LineWatchTB
     */
    public javax.swing.JTable getLineWatchTB() {
        return LineWatchTB;
    }

}
